package com.project.weimi.activity;

import java.util.HashMap;
import java.util.Map;

import com.project.weimi.R;
import com.project.weimi.application.Constant;
import com.project.weimi.entity.response.ResponseBean;
import com.project.weimi.interfaces.MyCallBack;
import com.project.weimi.utils.GsonUtil;
import com.project.weimi.utils.NetUtils;
import com.project.weimi.utils.XUtil;
import com.project.weimi.widget.EditTextWithDel;
import com.project.weimi.widget.ToastMessage;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

/**
 * 修改昵称Activity
 * @author Administrator
 *
 */
public class ChangeNickNameActivity extends BaseActivity{

	private EditTextWithDel et_nickName;
	private String userPhone;
	String nickName;
	String mNickName;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentViewWithBar(R.layout.activity_change_nickname, "更改昵称", true, "保存");
		
		// 获得从个人信息页面传来的昵称值
		mNickName = getIntent().getStringExtra("nickName");
				
		initView();
		
	}
	
	private void initView(){
		et_nickName = (EditTextWithDel)findViewById(R.id.et_nickName);
		bar.getRightBar().setOnClickListener(l);
		
		et_nickName.setText(mNickName);
	}
	
	// 保存
	private OnClickListener l = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			if(!NetUtils.isConnected(ChangeNickNameActivity.this)){
				ToastMessage.showToast(ChangeNickNameActivity.this, "网络不给力，请检查网络设置");
			} else {
				initHttp();
			}
			
		}
	};
	
	private void initHttp(){
		nickName = et_nickName.getText().toString().replace(" ", "").trim();
		Log.e("TAG", "请求数据中---ChangeNickNameActivity--"+nickName);
//		if(TextUtils.isEmpty(nickName)){
//			ToastMessage.showMyToast(ChangeNickNameActivity.this, "请输入昵称");
//			return;
//		}
		if(TextUtils.isEmpty(userPhone)){
			return;
		}
		if(nickName.equals(mNickName)){
			ToastMessage.showMyToast(ChangeNickNameActivity.this, "昵称与原来保持相同，未做修改");
			finish();
			return;
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("memberId", userPhone);
		map.put("userName", nickName);
		
		//Log.e("TAG", "请求数据中---ChangeNickNameActivity---userPhone=="+userPhone);
		
		XUtil.post(Constant.CHANGE_NICK_NAME, map, new MyCallBack<String>(){
			
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				if(null != result && !"".equals(result)){
					
					ResponseBean bean = (ResponseBean) GsonUtil.jsonToBean(
							result, ResponseBean.class);
					if(null != bean){
						String str = bean.getResult();
						if(!TextUtils.isEmpty(str) && "1".equals(str)){
							ToastMessage.showMyToast(ChangeNickNameActivity.this, "修改成功");
							Intent intent = new Intent();
							intent.putExtra("nickName", nickName);
							setResult(0, intent);
							finish();
						}
					}
				}
			}
			
			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
				super.onError(ex, isOnCallback);
			}
		});
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		if(null != spHelper && !TextUtils.isEmpty(memberId)){
			//Log.e("TAG", "onStart----ChangeNickNameActivity前memberId===---"+memberId);
			userPhone = memberId;
			//Log.e("TAG", "onStart----ChangeNickNameActivity后userPhone===---"+userPhone);
		}
	}
	
	@Override
	void setStatusBar(int statusBarColorRes) {
		super.setStatusBar(R.color.transparent_3);
	}
	
}
